package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Dict;
import com.atguigu.result.Result;
import com.atguigu.service.DictService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-24 16:09
 * @Description:
 */
@Controller
@RequestMapping("dict")
public class DictController {

    @Reference
    private DictService dictService;

    @RequestMapping
    public String index(){
        return "dict/index";
    }

    /*
    * 数据字典
    * 返回值是List<Map<String, Object>>；只是返回数据
    * 返回值是result 200,"成功" List<Map<String, Object>> 不仅仅有数据还有其他
    * */
    @RequestMapping("/findZnodes")
    @ResponseBody
    public Result findZnodes(
            @RequestParam(value = "id",defaultValue = "0") Long id)
    {
        List<Map<String, Object>> znodes = dictService.findZnodes(id);
        return Result.ok(znodes);
    }

    /**
     * 根据上级id获取子节点数据列表
     * @param parentId
     * @return
     */
    @RequestMapping("/findListByParentId/{parentId}")
    @ResponseBody
    public Result<List<Dict>> findListByParentId(@PathVariable Long parentId){
        List<Dict> dictList = dictService.findListByParentId(parentId);
        return Result.ok(dictList);
    }

    /**
     * 根据编码获取子节点数据列表
     * @param dictCode
     * @return
     */
    @RequestMapping("/findListByDictCode/{dictCode}")
    @ResponseBody
    public Result<List<Dict>> findListByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findListByDictCode(dictCode);
        return Result.ok(dictList);
    }


}
